/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package consumo.session;

import consumo.ejb.BasicSessionBean;
import consumo.entity.Tarifa;
import consumo.entity.TipoTarifa;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author Carlos Taschetto
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TarifaSession extends BasicSessionBean{

    public TipoTarifa getTipoTarifaById(int id) {
        return getPojo(TipoTarifa.class, id);
    }

    public List<TipoTarifa> getAllTipoTarifas(){
        return  getList(TipoTarifa.class, "Select c from TipoTarifa c");
    }
    
    public <T>List<T> getAllTarifa(Class<T> classToCast){
        return  getList(classToCast, "Select c from "+classToCast.getName().replace("consumo.entity.", "") +" c order by c.inicio desc");
    }
    
    public TipoTarifa getTipoTarifaByNome(String nome){
        return getPojo(TipoTarifa.class, "select c from TipoTarifa c where c.nome like ?1", "%"+nome+"%");
    }

    public List<Tarifa> getTipoTarifaByTipo(String nome){
        //System.out.println(getList(Tarifa.class, "select c from Tarifa c where c.tipoTarifa.descricao like ?1", "%"+nome+"%"));
        return getList(Tarifa.class, "select c from Tarifa c where c.tipoTarifa.descricao like ?1", "%"+nome+"%");
    }

    
    public TipoTarifa getTipoTarifaByAbreviatura(String nome){
        return getPojo(TipoTarifa.class, "select u from TipoTarifa u where u.abrev like ?1", "%"+nome+"%");
    }

    /**
     * Insere uma nova TipoTarifa
     * @param object
     * @return object
     */
    public <T>T save(Class<T> classToCast,T object){
        getEm().persist(object);
        return object;
    }

    /**
     * Altera dados do TipoTarifa
     * @param object
     * @return user editado
     */
    public <T>T set(Class<T> classToCast,T object){
        getEm().merge(object);
        return object;
    }

    public boolean removeTipoTarifa(int id){
        return (execute("Delete from TipoTarifa u where u.id = ?1", id) <= 0);
    }

    public void remove(Object object){
        removeEntity(object);
    }
}
